package com.ut.demo.widget;


import com.ut.demo.ResourceTable;
import net.steamcrafted.materialiconlib.factory.PixelMapBuilder;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.Path;
import ohos.agp.render.PixelMapHolder;
import ohos.agp.utils.Color;
import ohos.agp.utils.Rect;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;
import ohos.media.image.PixelMap;

/**
 * TODO: document your custom view class.
 */
public class SetBoundsTest extends Component implements Component.DrawTask, Component.EstimateSizeListener {

    private PixelMap pixelMap;

    private Rect bounds;

    private int padding = 30;

    public SetBoundsTest(Context context) {
        super(context);
        init();
    }

    public SetBoundsTest(Context context, AttrSet attrSet) {
        super(context, attrSet);
        init();
    }

    public SetBoundsTest(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        init();
    }

    public SetBoundsTest(Context context, AttrSet attrSet, int resId) {
        super(context, attrSet, resId);
        init();
    }

    private void init() {
        addDrawTask(this);
        setEstimateSizeListener(this);
    }

    @Override
    public boolean onEstimateSize(int i, int i1) {
        bounds = new Rect(0, 0, EstimateSpec.getSize(i), EstimateSpec.getSize(i1));
        setEstimatedSize(EstimateSpec.getChildSizeWithMode(bounds.getWidth(), bounds.getWidth(), EstimateSpec.NOT_EXCEED),
                EstimateSpec.getChildSizeWithMode(bounds.getHeight(), bounds.getHeight(), EstimateSpec.NOT_EXCEED));
        pixelMap = PixelMapBuilder.with(getContext())
                .setResId(ResourceTable.Media_numeric_7_box)
                .setWidth(bounds.getWidth()-padding*2)
                .setHeight(bounds.getHeight()-padding*2)
                .setIconColor(Color.RED.getValue())
                .build();
        return true;
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        Path path = new Path();
        path.addRect(new RectFloat(bounds), Path.Direction.CLOCK_WISE);
        canvas.clipPath(path, Canvas.ClipOp.INTERSECT);
        canvas.drawColor(0xFF000000, Canvas.PorterDuffMode.SRC);
        canvas.drawPixelMapHolder(new PixelMapHolder(pixelMap), padding, padding, new Paint());
    }

}
